package com.gupaoedu.bio;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;

public class BIOClient {
    /* 100条线程同时连接一个server并各自发送一个字符串给server */
    public static void main(String[] args) throws UnknownHostException, IOException {
        int count = 50;
        final CountDownLatch latch = new CountDownLatch(count);
        for (int i = 0; i < count; i++) {
            new Thread() {
                @Override
                public void run() {
                    try {
                        latch.await();
                        Socket client = new Socket("localhost", 8080);
                        OutputStream os = client.getOutputStream();
                        for (int i = 0; i < 4; i++) {
                            String name = Thread.currentThread().getName() + " " + new Date().getTime();
                            os.write(name.getBytes());
                        }
                        os.close();
                        client.close();
                    } catch (Exception ignored) {
                    }
                }
            }.start();
            latch.countDown();
        }
    }

}
